home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / kh_gdi.zip / APXPRINT.CPP < prev    next >
C/C++ Source or Header  |  1996-05-27  |  5KB  |  166 lines

  1. /*  Project slang
  2.     
  3.     Copyright ⌐ 1995. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    slang.exe Application
  6.     FILE:         APXPrint.CPP
  7.     AUTHOR:       Stepan S.Vartanov
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of Printing.
  13. */
  14.  
  15.  
  16. #include <owl\owlpch.h>
  17. #pragma hdrstop
  18.  
  19. #include "apxprint.h"
  20.  
  21.  
  22. // Do not enable page range in the print dialog since only one page is
  23. // available to be printed
  24. //
  25. void APXPrintOut::GetDialogInfo (int& minPage, int& maxPage, int& selFromPage, int& selToPage)
  26. {
  27.     minPage = maxPage = 0;
  28.     selFromPage = selToPage = 0;
  29. }
  30.  
  31.  
  32. void APXPrintOut::BeginPrinting ()
  33. {
  34.     TRect clientR;
  35.  
  36.     BeginPage(clientR);
  37.  
  38.     HFONT   hFont = (HFONT)Window->GetWindowFont();
  39.     TFont   font("Arial", -12);
  40.     if (hFont == 0)
  41.       DC->SelectObject(font);
  42.     else
  43.       DC->SelectObject(TFont(hFont));
  44.     
  45.     TEXTMETRIC  tm;
  46.     int fHeight = (DC->GetTextMetrics(tm) == true) ? tm.tmHeight + tm.tmExternalLeading : 10;
  47.  
  48.     DC->RestoreFont();
  49.     
  50.     // How many lines of this font can we fit on a page.
  51.     int linesPerPage = MulDiv(clientR.Height(), 1, fHeight);
  52.     
  53.     TPrintDialog::TData &printerData = Printer->GetSetup();
  54.  
  55.     // GetMinMaxInfo event is overrided to return the number of lines when printing.
  56.     MINMAXINFO minmaxinfo;
  57.     Window->SendMessage(WM_GETMINMAXINFO, 0, (long)&minmaxinfo);
  58.     int maxPg = ((minmaxinfo.ptMaxSize.y / linesPerPage) + 1.0);
  59.  
  60.  
  61.     // Compute the number of pages to print.
  62.     printerData.MinPage = 1;
  63.     printerData.MaxPage = maxPg;
  64.  
  65.     EndPage();
  66.  
  67.     TPrintout::BeginPrinting();
  68. }
  69.  
  70.  
  71. void APXPrintOut::BeginPage (TRect &clientR)
  72. {
  73.     TScreenDC screenDC;
  74.     TSize screenRes(screenDC.GetDeviceCaps(LOGPIXELSX),
  75.                     screenDC.GetDeviceCaps(LOGPIXELSY));
  76.     TSize printRes(DC->GetDeviceCaps(LOGPIXELSX),
  77.                    DC->GetDeviceCaps(LOGPIXELSY));
  78.  
  79.     // Temporarily change the window size (so any WM_PAINT queries on the total window size (GetClientRect) is
  80.     // the window size for the WM_PAINT of the window and the printer page size when Paint is called from
  81.     // PrintPage. Notice, we don't use AdjustWindowRect because its harder and not accurate.  Instead we
  82.     // compute the difference (in pixels) between the client window and the frame window.  This difference
  83.     // is then added to the clientRect to compute the new frame window size for SetWindowPos.
  84.     clientR = Window->GetClientRect();
  85.     Window->MapWindowPoints(HWND_DESKTOP, (TPoint*)&clientR, 2);
  86.  
  87.     // Compute extra X and Y pixels to bring a client window dimensions to equal the frame window.
  88.     OrgR = Window->GetWindowRect();
  89.     int adjX = OrgR.Width() - clientR.Width();
  90.     int adjY = OrgR.Height() - clientR.Height();
  91.     
  92.     // Conditionally scale the DC to the window so the printout will resemble the window.
  93.     if (Scale) {
  94.         clientR = Window->GetClientRect();
  95.         PrevMode = DC->SetMapMode(MapMode);
  96.         DC->SetViewportExt(PageSize, &OldVExt);
  97.  
  98.         // Scale window to logical page size (assumes left & top are 0)
  99.         clientR.right = MulDiv(PageSize.cx, screenRes.cx, printRes.cx);
  100.         clientR.bottom = MulDiv(PageSize.cy, screenRes.cy, printRes.cy);
  101.  
  102.         DC->SetWindowExt(clientR.Size(), &OldWExt);
  103.     }
  104.  
  105.     // Compute the new size of the window based on the printer DC dimensions.    
  106.     // Resize the window, notice position, order, and redraw are not done the window size changes but the user
  107.     // doesn't see any visible change to the window.
  108.     Window->SetRedraw(false);
  109.     Window->SetWindowPos(0, 0, 0, clientR.Width() + adjX, clientR.Height() + adjY,
  110.                          SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  111. }
  112.  
  113.  
  114. void APXPrintOut::PrintPage (int page, TRect& bandRect, unsigned)
  115. {
  116.     TRect clientR;
  117.  
  118.     BeginPage(clientR);
  119.  
  120.     if (Scale)
  121.         DC->DPtoLP(bandRect, 2);
  122.  
  123.     // Change the printer range to this current page.
  124.     TPrintDialog::TData& printerData = Printer->GetSetup();
  125.     int fromPg = printerData.FromPage;
  126.     int toPg = printerData.ToPage;
  127.  
  128.     printerData.FromPage = page;
  129.     printerData.ToPage = page;
  130.  
  131.     // Call the window to paint itself to the printer DC.
  132.     Window->Paint(*DC, false, bandRect);
  133.  
  134.     printerData.FromPage = fromPg;
  135.     printerData.ToPage = toPg;
  136.  
  137.     if (Scale)
  138.         DC->LPtoDP(bandRect, 2);
  139.  
  140.     EndPage();
  141. }
  142.  
  143.  
  144. void APXPrintOut::EndPage ()
  145. {
  146.     // Resize to original window size, no one's the wiser.
  147.     Window->SetWindowPos(0, 0, 0, OrgR.Width(), OrgR.Height(),
  148.                          SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  149.     Window->SetRedraw(true);
  150.  
  151.     // Restore changes made to the DC
  152.     if (Scale) {
  153.         DC->SetWindowExt(OldWExt);
  154.         DC->SetViewportExt(OldVExt);
  155.         DC->SetMapMode(PrevMode);
  156.     }
  157. }
  158.  
  159.  
  160. bool APXPrintOut::HasPage (int pageNumber)
  161. {
  162.     TPrintDialog::TData &printerData = Printer->GetSetup();
  163.  
  164.     return (pageNumber >= printerData.MinPage) && (pageNumber <= printerData.MaxPage);
  165. }
  166.